package com.power.using.mybomb.adapter;

import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMMessageType;
import cn.bmob.v3.BmobUser;

/**
 * Created by 聪明一只哈 on 2020/3/24.
 * Time: 2020/3/24  10:03
 */
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //文本
    private final int TYPE_RECEIVER_TXT = 0;
    private final int TYPE_SEND_TXT = 1;
    //图片
    private final int TYPE_SEND_IMAGE = 2;
    private final int TYPE_RECEIVER_IMAGE = 3;
    //位置
    private final int TYPE_SEND_LOCATION = 4;
    private final int TYPE_RECEIVER_LOCATION = 5;
    //语音
    private final int TYPE_SEND_VOICE =6;
    private final int TYPE_RECEIVER_VOICE = 7;
    //视频
    private final int TYPE_SEND_VIDEO =8;
    private final int TYPE_RECEIVER_VIDEO = 9;

    //同意添加好友成功后的样式
    private final int TYPE_AGREE = 10;

    private final long TIME_INTERVAL=10*60*1000;

    private List<BmobIMMessage> msgs=new ArrayList<>();
    private BmobIMConversation c;
    private String currentUid="";


    public ChatAdapter(Context context, BmobIMConversation c) {
        try {
            currentUid = BmobUser.getCurrentUser(BmobUser.class).getObjectId();
        }catch (Exception e){
            e.printStackTrace();
        }
        this.c = c;
    }

    public int findPosition(BmobIMMessage message){
        int index=this.getCount();
        int position=-1;
        while (index-- >0){
            if(message.equals(this.getItem(index))){
                position=index;
                break;
            }
        }
        return position;
    }

    public int findPosition(long id){
        int index=this.getCount();
        int position=-1;
        while(index-->0){
            if(this.getItemId(index)==id){
                position=index;
                break;
            }
        }
        return position;
    }

    public int getCount(){
        return this.msgs==null?0:this.msgs.size();
    }

    public void addMessages(List<BmobIMMessage> messages){
        msgs.addAll(0,messages);
        notifyDataSetChanged();
    }

    public void addMessage(BmobIMMessage message){
        msgs.addAll(Arrays.asList(message));
        notifyDataSetChanged();
    }


    /**
     * 获取消息
     * @param position
     * @return
     */
    public BmobIMMessage getItem(int position){
        return this.msgs==null?null:(position>=this.msgs.size()?null:this.msgs.get(position));
    }

    /**
     * 移除消息
     * @param position
     */
    public void remove(int position){
        msgs.remove(position);
        notifyDataSetChanged();
    }

    public BmobIMMessage getFirstMessage(){
        if(null!=msgs&&msgs.size()>0){
            return msgs.get(0);
        }else{
            return null;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if(viewType==TYPE_SEND_TXT){
            return new SendTextHolder(parent.getContext(),parent,c,onRecyclerViewListener);
        }else{
            return null;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((BaseViewHolder)holder).bindData(msgs.get(position));

        if(holder instanceof ReceiveTextHolder){
            ((ReceiveTextHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof ReceiveImageHolder){
            ((ReceiveImageHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof ReceiveLocationHolder){
            ((ReceiveLocationHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof ReceiveVoiceHolder){
            ((ReceiveVoiceHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof SendTextHolder){
            ((SendTextHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof SendImageHolder){
            ((SendImageHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof SendLocationHolder){
            ((SendLocationHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof SendVoiceHolder){
            ((SendVoiceHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof SendVideoHolder){
            ((SendVideoHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof ReceiveVideoHolder){
            ((ReceiveVideoHolder)holder).showTime(shouldShowTime(position));
        }else if(holder instanceof AgreeHolder){
            ((AgreeHolder)holder).showTime(shouldShowTime(position));
        }
    }

    @Override
    public int getItemViewType(int position) {

        BmobIMMessage message = msgs.get(position);
        if(message.getMsgType().equals(BmobIMMessageType.IMAGE.getType())){
            return message.getFromId().equals(currentUid)?TYPE_SEND_IMAGE:TYPE_RECEIVER_IMAGE;
        }else if(message.getMsgType().equals(BmobIMMessageType.LOCATION.getType())){
            return message.getFromId().equals(currentUid)?TYPE_SEND_LOCATION:TYPE_RECEIVER_LOCATION;
        }else if(message.getMsgType().equals(BmobIMMessageType.VOICE.getType())){
            return message.getFromId().equals(currentUid)?TYPE_SEND_VOICE:TYPE_RECEIVER_VOICE;
        }else if(message.getMsgType().equals(BmobIMMessageType.TEXT.getType())){
            return message.getFromId().equals(currentUid)?TYPE_SEND_TXT:TYPE_RECEIVER_TXT;
        }else if(message.getMsgType().equals(BmobIMMessageType.VIDEO.getType())){
            return message.getFromId().equals(currentUid)?TYPE_SEND_VIDEO:TYPE_RECEIVER_VIDEO;
        }else if(message.getMsgType().equals("agree")){
            return TYPE_AGREE;
        }else{
            return -1;
        }

    }

    @Override
    public int getItemCount() {
        return msgs.size();
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private boolean shouldShowTime(int position){
        if(position==0){
            return true;
        }
        long lastTime=msgs.get(position-1).getCreateTime();
        long curTime=msgs.get(position).getCreateTime();
        return curTime-lastTime>TIME_INTERVAL;
    }
}
